package com.tjsinfo.tjsoa.system.bo;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ISystemDutyDao;
import com.tjsinfo.tjsoa.system.vo.TSystemDuty;

public class SystemDutyBo implements ISystemDutyBo {
	private IHibernateDao hibernateDao;
	private IQbcDao qbcDao;
	private ISystemDutyDao systemDutyDao;
	
	public ISystemDutyDao getSystemDutyDao() {
		return systemDutyDao;
	}

	public void setSystemDutyDao(ISystemDutyDao systemDutyDao) {
		this.systemDutyDao = systemDutyDao;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public Integer addSystemDuty(TSystemDuty systemDuty) {
		// TODO Auto-generated method stub
		return hibernateDao.save(systemDuty);
	}

	public Boolean deleteSystemDuty(String id) {
		// TODO Auto-generated method stub
		try {
			TSystemDuty systemDuty = selectById(id);
			hibernateDao.delete(systemDuty);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

	public List<TSystemDuty> selectAll() {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDuty.class);
		Byte state = 0;
		criteria.add(Restrictions.eq("isDel", state));
		return qbcDao.getCriteria(criteria);
	}

	public TSystemDuty selectById(String id) {
		// TODO Auto-generated method stub
		return systemDutyDao.findById(id);
	}

	public List<TSystemDuty> selectDutyPage(Integer first, Integer max) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDuty.class);
		
		return qbcDao.findPageByCriteria(criteria, max, first).getItems();
	}

	public Integer selectDutyTotalLine() {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDuty.class);
		return qbcDao.getQueryNumber(criteria, "dutyId");
	}

	public void save(TSystemDuty transientInstance) {
		// TODO Auto-generated method stub
		systemDutyDao.save(transientInstance);
	}

	public boolean update(TSystemDuty detachedInstance) {
		// TODO Auto-generated method stub
		return systemDutyDao.update(detachedInstance);
	}

	public TSystemDuty findById(String id) {
		// TODO Auto-generated method stub
		return systemDutyDao.findById(id);
	}
	
	public Boolean intoSystemDuty(String postId){
		TSystemDuty tsd = findById(postId);
		if (tsd.getIsDel() == 1) {
			Byte state = 0;
			tsd.setIsDel(state);
		} else {
			Byte state = 1;
			tsd.setIsDel(state);
		}
		return  update(tsd);
	}

	public boolean isSameNameDuty(String dutyname) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDuty.class);
		criteria.add(Restrictions.eq("dutyName", dutyname));
		if (qbcDao.getCriteria(criteria).size() > 0) {
			return true;
		} else {
			return false;
		}
		
		
	}

	public boolean isSameNameDuty(String dutyname, String id) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDuty.class);
		criteria.add(Restrictions.eq("dutyName", dutyname));
		criteria.add(Restrictions.not(Restrictions.eq("dutyId", id)));
		if (qbcDao.getCriteria(criteria).size() > 0) {
			return true;
		} else {
			return false;
		}
		
	}	
	public TSystemDuty selectByName(String dutyName){
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemDuty.class);
		criteria.add(Restrictions.eq("dutyName", dutyName));
		List<TSystemDuty> list = qbcDao.getCriteria(criteria);
		if (list.size() > 0) {
			return list.get(0);
			
		} else {
			return new TSystemDuty();
		}
	}
}
